package leetcode.lt100;

class Solution_83 {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.append(2).append(2).append(3).append(4).append(4).append(5).append(5).append(5).append(5).append(5).append(5);
        System.out.println(deleteDuplicates(head));
    }

    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode currentNode = head.next;
        while (currentNode != null && head.val == currentNode.val) {
            currentNode = currentNode.next;
        }
        head.next = deleteDuplicates(currentNode);
        return head;
    }

    /**
     * Definition for singly-linked list.
     **/
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            if (next == null) {
                return val + "";
            } else {
                return val + "," + next.toString();
            }
        }

        public ListNode append(int val) {
            next = new ListNode(val);
            return next;
        }
    }

}